
function showMsg(info,timeout) {
	var timeout = timeout?timeout:1000;
	$.messager.show({    // show error message
        title: '系统提示',
        msg: info,
        timeout:timeout,
        showType:'slide'
    });
}
function openMsg(options) {
	var width = options.width?options.width:250;
	var height = options.height?options.height:100;
	var timeout = options.timeout==0?options.timeout:1000;
	var icon = options.icon?options.icon:'info';
//	if(options.type=='alert') {
//		icon:显示icon图片,可用值有: error,question,info,warning.
//		$.messager.alert({    // show error message
//	        title: options.title,
//	        msg: options.msg,
//	        icon:'warning',
//	        width:width,
//			height:height,
//	        timeout:timeout,
//	        showType:'slide',
//	        position:options.position
//	    });
//	}else {
	var win =  $.messager.show({    // show error message
	        title: options.title,
	        msg: options.msg,
	        icon:icon,
	        width:width,
			height:height,
	        timeout:timeout,
	        showType:'slide',
	        position:options.position
	    });
	return win;
//	}
	
}
/**
 * 操作成功提示
 */
function showSucMsg() {
	showMsg('操作成功.');
}
/**
 * 操作失败提示
 */
function showFailMsg() {
	showMsg('操作失败.');
}
function showError(info) {
	
}

function alertMsg(info) {
	$.messager.alert('系统提示',info);
}

function printLog(info) {
	console.log(info);
}

function isEmpty(v) {
	switch (typeof v) {
	case 'undefined':
		return true;
	case 'string':
		if (trim(v).length == 0)
			return true;
		break;
	case 'boolean':
		if (!v)
			return true;
		break;
	case 'number':
		if (0 === v)
			return true;
		break;
	case 'object':
		if (null === v)
			return true;
		if (undefined !== v.length && v.length == 0)
			return true;
		for ( var k in v) {
			return false;
		}
		return true;
		break;
	}
	return false;
}

function isNotEmpty(val) {
	if (typeof(val) == "undefined") { 
		return false;
	}else if(val==null) {
		return false;
	}else if(val==''||val=='null') {
		return false;
	}else {
		return true;
	}
	
	 
}
 


function onloading(){  
    $("<div class=\"datagrid-mask\"></div>").css({display:"block",width:"100%",height:$(window).height()}).appendTo("body");   
    $("<div class=\"datagrid-mask-msg\"></div>").html("数据处理中，请稍候...").appendTo("body").css({display:"block","z-index": 9999 ,left:($(document.body).outerWidth(true) - 190) / 2,top:($(window).height() - 45) / 2});   
}  
function removeload(){  
   $(".datagrid-mask").remove();  
   $(".datagrid-mask-msg").remove();  
}

function onLoadingHandler(executeFunc, isAutoClose)
{
	onloading();
	//sleep(4000);
	//window.setTimeout(executeFunc,500);//设定最小等待时间
	executeFunc();
	if(isAutoClose)//如果执行函数中没有消息提示，则需自动关闭
	{
		removeload();
	}
}

function downLoadFile(url) {
		if($('#downloadForm')) $('#downloadForm').remove();
		var downloadForm  = $("<form id=\"downloadForm\">");//定义一个form表单
		downloadForm.attr("style","display:none");
		downloadForm.attr("target","");
		downloadForm.attr("method","post");
		downloadForm.attr("action",url);
		var input1=$("<input>");
		input1.attr("type","hidden");
		input1.attr("name","downLoad");
		input1.attr("value",(new Date()).getMilliseconds()); 
		$("body").append(downloadForm);//将表单放置在web中
		downloadForm.append(input1);
		downloadForm.submit();//表单提交
		return false;
}



/**
 * @author zzx
 * 
 * 增加formatString功能
 * 
 * 使用方法：$.formatString('字符串{0}字符串{1}字符串','第一个变量','第二个变量');
 * 
 * @returns 格式化后的字符串
 */
$.formatString = function(str) {
	for ( var i = 0; i < arguments.length - 1; i++) {
		str = str.replace("{" + i + "}", arguments[i + 1]);
	}
	return str;
};
//默认自动提交表单
function createFromWindow(title,href,submitUrl,callback,enableSaveButton,width,height) {
	var winWidth = width?width:800;
	var winHeight = height?height:500;
	$.easyui.showDialog({
        title: title,
        maximizable: true,
        autoRestore: true,
        width : winWidth,
		height : winHeight,
        topMost: false,
		enableApplyButton:false,
		enableSaveButton:enableSaveButton,
        href: href,
        onSave: function (d) {
			printLog(d);
            var validate = d.form("validate");
            printLog(validate);
            if (validate) {
           	    printLog(d.form("getData"));
	           	$.ajax({
	           	  type: 'POST',
	           	  url: submitUrl,
	              dataType : 'json',  
	           	  data: d.form("getData"),
	           	  success: function(data) {
	           		  printLog(data);
	           		  showMsg(data.msg);
	           		  if(data.success) {
		           		callback(data);
		           	  }
	           	  },
		          error:function(data,textstatus){  
		        	  printLog(data);
		        	  showMsg('请求异常');
		           }  
	           	});
            } else {
                return false;
            }
        },
    });
}
//创建自动提交的表单，直接点保存提交
function createFromAutoSubmitWindow(options) {
	var winWidth = options.width?options.width:800;
	var winHeight = options.height?options.height:500;
	$.easyui.showDialog({
        title: options.title,
        maximizable: options.maximizable,
        autoRestore: true,
        width : winWidth,
		height : winHeight,
        topMost: false,
		enableApplyButton:false,
		enableSaveButton:options.enableSaveButton,
        href: options.href,
        onSave: function (d) {
			printLog(d);
            var validate = d.form("validate");
            printLog(validate);
            if (validate) {
           	    printLog(d.form("getData"));
	           	$.ajax({
	           	  type: 'POST',
	           	  dataType : 'json',  
	           	  url: options.submitUrl,
	           	  data: d.form("getData"),
	           	  success: function(data) {
	           		  printLog(data);
	           		  showMsg(data.msg);
	           		  if(data.success) {
	           			options.callback(data);
		           	  }
	           	  },
		          error:function(data,textstatus){  
		        	  printLog(data);
		        	  showMsg('请求异常');
		          } 
	           	});
            } else {
                return false;
            }
        },
    });
}
//创建提供回调的保存表单
function createFromWindowOptions(options) {
	var winWidth = options.width?options.width:800;
	var winHeight = options.height?options.height:500;
	var saveButtonText = options.saveButtonText?options.saveButtonText:'保存';
	return $.easyui.showDialog({
        title: options.title,
        maximizable: options.maximizable,
        autoRestore: true,
        width : winWidth,
		height : winHeight,
        topMost: false,
		enableApplyButton:false,
		enableSaveButton:options.enableSaveButton,
        href: options.href,
        saveButtonText:saveButtonText,
        onSave: function (d) {
        	 printLog(d);
        	 var validate = d.form("validate");
             printLog(validate);
             if (validate) {
            	 return options.callback(d);
             } else {
                 return false;
             }
        	
        },
    });
}
//$.easyui.showDialog({
//    title: "顾客入住信息",
//    width: 800, 
//    height: 500,
//    content: "这是通过 $.easyui.showDialog 方法创建的 Dialog，该窗口具有自定的工具栏和按钮栏",
//    autoRestore: true,
//    applyButtonText: "按钮a",
//    closeButtonText: "关闭按钮",
//    saveButtonText:"离开",
//    applyButtonIconCls: "icon-search",
//    enableSaveButton: true,
//    toolbar: [
//        { text: "button1", iconCls: "icon-hamburg-phone", handler: function () { alert("button1"); } },
//        { text: "button2", handler: function () { alert("button2"); } },
//        "-",
//        { text: "button3", iconCls: "icon-hamburg-pencil", handler: function () { alert("button3"); } }
//    ],
//    buttons: [
//        { text: "按钮1", iconCls: "icon-hamburg-pencil", handler: function () { alert("测试按钮1"); } },
//        { text: "按钮2", handler: function () { alert("测试按钮2"); } }
//    ]
//});


/**
 * @author zzx
 * 
 * @requires jQuery,EasyUI
 * 
 * 创建一个模式化的dialog
 * 
 * @returns $.modalDialog.handler 这个handler代表弹出的dialog句柄
 * 
 * @returns $.modalDialog.xxx 这个xxx是可以自己定义名称，主要用在弹窗关闭时，刷新某些对象的操作，可以将xxx这个对象预定义好
 */
$.modalDialog = function(options) {
	if ($.modalDialog.handler == undefined) {// 避免重复弹出
	var opts = $.extend({
		title : '',
		maximizable: true,
	    autoRestore: true,
		width : 840,
		height : 680,
		modal : true,
		onClose : function() {
			$.modalDialog.handler = undefined;
			$(this).dialog('destroy');
		},
		onOpen : function() {
//				parent.$.messager.progress({
//					text : '数据加载中，请稍后....'
//				});
//				onloading();
		}
	}, options);
	opts.modal = true;// 强制此dialog为模式化，无视传递过来的modal参数
	return $.modalDialog.handler = $('<div/>').dialog(opts);
	}
};

/**
 * 设置表格查询条件
 * 2015-4-17下午10:58:14
 * @param gridId 列表id
 * @param formId 表单id
 * @param isClear 是否清空
 */
function setGridSearchParams(gridId,formId,isClear,callback) {
	var fields =$('#'+formId).serializeArray(); //自动序列化表单元素为JSON对象  
	printLog(fields);
	var params = $('#'+gridId).datagrid('options').queryParams; //先取得 datagrid 的查询参数  
    $.each(fields, function(i, field){ 
    	if(isClear) {
    		delete params[field.name];
    	}else {
    		params[field.name] = field.value; //设置查询参数
    	}
    });
    if(isClear) {
    	$('#'+formId).form('clear');
    }
    callback();
}

function isFirefox()
{
    return (navigator.userAgent.indexOf("Firefox")!=-1);
}
function isChrome()
{
    return (navigator.userAgent.indexOf("Chrome")!=-1);
}

function jmaxAjax(url,data,callback,noMsg) {
	$.ajax({
	 	type: 'POST',
	 	dataType : 'json',  
	 	url: url,
	 	data: data,
	 	success: function(data) {
	 		printLog(data);
	 		if(!noMsg) {
	 			showMsg(data.msg);
	 		}
	 		
	 		if(data.success&&callback) {
	 			callback(data);
	     	}
	 	},
	    error:function(data,textstatus){  
		  	printLog(data);
		  	showMsg('请求异常');
		}
    });
}
function jmaxSyncAjax(url,data,callback) {
	$.ajax({
	 	type: 'POST',
	 	dataType : 'json', 
	 	async:false,
	 	url: url,
	 	data: data,
	 	success: function(data) {
	 		printLog(data);
//	 		showMsg(data.msg);
//	 		if(data.success&&callback) {
	 			callback(data);
//	     	}
	 	},
	    error:function(data,textstatus){  
		  	printLog(data);
		  	showMsg('请求异常');
		}
    });
}


